const handleReply = function (res, text) {
    if (text) {
        return res.reply(text);
    } else {
        return res.reply({
            type: 'text',
            content: '如需要任何帮助,请直接电话联系客服'
        });
    }
}

module.exports = function (settings, storage) {
    return {
        /**
         * 取消关注公众号
         */
        unsubscribe: function (message, req, res) {
            let openid = message.FromUserName;
            storage.models.Users.update({subscribe: 0}, {where: {openid: openid}});

            handleReply(res, settings.value('Event_unsubscribe') || '取消关注成功');
        },

        /**
         * 关注公众号
         */
        subscribe: function (message, req, res) {
            let body;
            if (message.EventKey && message.EventKey.indexOf('qrscene') != -1) {
                body = {inviter_id: message.EventKey.split('_')[1]};
            }
            return storage.wechatInstance.getUserByOpenid(message.FromUserName, body, true).then(function (result) {
                if (result.success) {
                    let user = result.data;
                    return storage.models.Users.findOne({
                        where: {id: user.agency_id || 1},
                        attributes: ['id', 'order_return_scale']
                    }).then(function (agency) {
                        if (agency && agency.order_return_scale > 0) {
                            handleReply(res, [{
                                "title": "如何使用",
                                "description": "便宜咯每天为您更新几万个商家活动商品，更有9.9包邮，白菜价2.9包邮，为你提供最省钱的购物途径。确认收货后，不需要截图，也不需要评价，返利直接送到您手上！",
                                "picurl": "https://mmbiz.qlogo.cn/mmbiz_jpg/RK7A330w2XIRIibfk8pewSDUc5S5IH1OicMfAHu64aVUn9ehMEBibB3bPXicOEb6TYjhoOETLaCnL7fdFl82OQcVkw/0?wx_fmt=jpeg",
                                "url": "https://mp.weixin.qq.com/s/smbB6BW7h46mBDa4P918BQ"
                            }]);
                        } else {
                            handleReply(res, [{
                                "title": "新手必看",
                                "description": "便宜咯每天为您更新几万个商家活动商品，更有9.9包邮，白菜价2.9包邮，为你提供最省钱的购物途径。",
                                "picurl": "https://mmbiz.qlogo.cn/mmbiz_jpg/RK7A330w2XIRIibfk8pewSDUc5S5IH1OicibztticjfXQuNVf1ZezpicWa0rWRth2IGMOT5mbwOG1p4lSXYXE6fRtxA/0?wx_fmt=jpeg",
                                "url": "https://mp.weixin.qq.com/s/2hHKjih-ew92LBBNuwWWUA"
                            }]);
                        }
                    });
                } else {
                    handleReply(res, {
                        type: 'text',
                        content: '感谢您关注我们'
                    });
                }

                // if (result.new) {
                //     storage.wechatInstance.shopWebApi.sendText(message.FromUserName, '感谢您成为' + result.data.id + '号会员', function (err) {
                //         if (err) {
                //             console.error('\n', err.stack || err.message);
                //         }
                //     });
                // }
            });
        },

        /**
         * 扫一扫进入公众号
         */
        scan: function (message, req, res) {
            storage.wechatInstance.getUserByOpenid(message.FromUserName);
            return res.reply();
        }
    };
}